package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbGroupJoin;
import com.bwie.service.TbGroupJoinService;
import com.bwie.mapper.TbGroupJoinMapper;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
* @author 牛y
* @description 针对表【tb_group_join】的数据库操作Service实现
* @createDate 2025-07-26 11:37:16
*/
@Service
public class TbGroupJoinServiceImpl extends ServiceImpl<TbGroupJoinMapper, TbGroupJoin>
    implements TbGroupJoinService{

    @Autowired
    TbGroupJoinMapper tbGroupJoinMapper;

    @Autowired
    FastFileStorageClient fileStorageClient;

    @Override
    public R getGroupJoinList(PageInfoVo pageInfoVo) {
        Page<TbGroupJoin> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
//        QueryWrapper<TbGroupJoin> wrapper = new QueryWrapper<>();
//        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
//            wrapper.lambda().like(TbGroupJoin::getJoinName,pageInfoVo.getKeyWord());
//        }
//        if(StringUtils.isNotEmpty(pageInfoVo.getBeginTime())){
//            wrapper.lambda().ge(TbGroupJoin::getBeginTime,pageInfoVo.getBeginTime());
//        }
//        if(StringUtils.isNotEmpty(pageInfoVo.getEndTime())){
//            wrapper.lambda().le(TbGroupJoin::getBeginTime,pageInfoVo.getEndTime());
//        }
        Page<TbGroupJoin> selectPage = tbGroupJoinMapper.getGroupJoinList(page, pageInfoVo);
        for (TbGroupJoin record : selectPage.getRecords()) {
            String joinImages = record.getJoinImages();
            String[] split = joinImages.split(",");
            record.setImageList(Arrays.asList(split));
        }
        return R.OK(selectPage);
    }

    @Override
    public R uploadFile(MultipartFile file) {


        try {
           String extName = FileNameUtil.extName(file.getOriginalFilename());
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null);
            String url="http://192.168.80.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (Exception e) {
//            throw new RuntimeException(e);
            return R.ERROR(500,"图片上传失败");
        }

    }

    @Override
    public R addGroupJoin(TbGroupJoin tbGroupJoin) {
        TbGroupJoin groupJoin = tbGroupJoinMapper.selectOne(new QueryWrapper<TbGroupJoin>().lambda().eq(TbGroupJoin::getJoinName, tbGroupJoin.getJoinName()));
        if(groupJoin!=null){
            return R.ERROR(500,"活动名称重复");
        }
        tbGroupJoinMapper.insert(tbGroupJoin);
        return R.OK();
    }

    @Override
    public R deleteById(Integer joinId) {
        TbGroupJoin groupJoin = tbGroupJoinMapper.selectById(joinId);
        if(groupJoin==null){
            return R.ERROR(500,"活动为空");
        }
        tbGroupJoinMapper.deleteById(joinId);
        return R.OK();
    }

    @Override
    public R updateGroupJoin(TbGroupJoin tbGroupJoin) {
        TbGroupJoin groupJoin = tbGroupJoinMapper.selectOne(new QueryWrapper<TbGroupJoin>().lambda().eq(TbGroupJoin::getJoinId, tbGroupJoin.getJoinId()));
        if(groupJoin==null){
            return R.ERROR(500,"活动不存在");
        }
        tbGroupJoinMapper.updateById(tbGroupJoin);
        return R.OK();
    }
}




